package com.duoduo.springboot.security.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.duoduo.springboot.security.dao.UserDao;
import com.duoduo.springboot.security.model.User;
import com.duoduo.springboot.security.vo.UserVO;

@Service
public class UserService {

	@Autowired
	private UserDao userDao;

	public List<UserVO> listAll() {
		List<UserVO> result = new ArrayList<UserVO>(0);
		List<User> userList = userDao.listAll();
		if (!CollectionUtils.isEmpty(userList)) {
			for (User user : userList) {
				result.add(UserVO.fromEntity(user));
			}
		}
		return result;
	}

	public UserVO getById(Integer userId) {
		User user = userDao.getById(userId);
		return (user != null) ? UserVO.fromEntity(user) : null;
	}

	public UserVO getByAccount(String account) {
		User user = userDao.getByAccount(account);
		return (user != null) ? UserVO.fromEntity(user) : null;
	}
}
